package com.web.kdl.common.result;

import lombok.Data;

/**
 * @Author： shulibin
 * @Date： 2025/7/21 11:34
 * @Describe：
 */
@Data
public class CommonResult<T> {
    private  Integer code;
    private  String message;
    private  T data;
    private long timestamp;//接口请求时间
    public CommonResult(){
        timestamp = System.currentTimeMillis();
    }

    public CommonResult(int code, String message) {
        this.code = code;
        this.message =message;
    }
    public CommonResult(ReturnCode resultEnum) {
        this.code = resultEnum.getCode();
        this.message = resultEnum.getMessage();
    }

    public CommonResult(int code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }


    public  static <T> CommonResult<T>success(T data){
        CommonResult<T> r = new CommonResult<>();
        r.code = ReturnCode.SUCCESS.getCode();
        r.setMessage(ReturnCode.SUCCESS.getMessage());
        r.setData(data);
        return r;
    }
    public  static <T> CommonResult<T>success(){
        CommonResult<T> r = new CommonResult<>();
        r.code = ReturnCode.SUCCESS.getCode();
        r.setMessage(ReturnCode.SUCCESS.getMessage());
        r.setData(null);
        return r;
    }
    public static <T> CommonResult<T> success(String message,T data){
        return new CommonResult<T>(ReturnCode.SUCCESS.getCode(),message,data);
    }
    public static <T> CommonResult<T>error(int code, String message){
        CommonResult<T> r = new CommonResult<>();
        r.setCode(code);
        r.setMessage(message);
        return r;
    }
    public static <T> CommonResult<T>error(String message){
        CommonResult<T> r = new CommonResult<>();
        r.setCode(ReturnCode.FAIL.getCode());
        r.setMessage(message);
        return r;
    }
    public static <T> CommonResult<T>error(int code){
        CommonResult<T> r = new CommonResult<>();
        r.setCode(code);
        ReturnCode returnCode = ReturnCode.valueOfCode(code);
        r.setMessage(returnCode.getMessage());
        return r;
    }
}
